package com.miyake.loveviewweb.client.plugin;

import java.util.List;

import com.google.appengine.api.datastore.Key;
import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
import com.miyake.loveviewweb.client.dialog.BasicDialog;
import com.miyake.loveviewweb.client.dialog.DialogHandler;
import com.miyake.loveviewweb.client.dialog.IDialogPanel;
import com.miyake.loveviewweb.client.service.DataService;
import com.google.gwt.user.client.ui.Button;

public class PluginListCellTable extends Composite implements HasText, IPluginList, IDialogPanel {

    private static PluginListUiBinder uiBinder = GWT
        .create(PluginListUiBinder.class);

    interface PluginListUiBinder extends UiBinder<Widget, PluginListCellTable> {
    }

    public PluginListCellTable() {
        initWidget(uiBinder.createAndBindUi(this));
        initialize();
    }
    @UiField(provided=true) CellTable<PluginModelEx> cellTable = new CellTable<PluginModelEx>();
    @UiField Button buttonSearch;
    private List<PluginModelEx> data;

    public PluginListCellTable(String firstName) {
        initWidget(uiBinder.createAndBindUi(this));
        initialize();
    }

    private void initialize() {
        cellTable.setRowCount(0);
        Column<PluginModelEx, Boolean> checkColumn = new Column<PluginModelEx, Boolean>(
                new CheckboxCell(true, false)) {
              @Override
              public Boolean getValue(PluginModelEx object) {
                // Get the value from the selection model.
                return true;//selectionModel.isSelected(object);
              }
        };
        cellTable.addColumn(checkColumn, SafeHtmlUtils.fromSafeConstant("<br/>"));
        
        TextColumn<PluginModelEx> nameColumn = new TextColumn<PluginModelEx>(){
            @Override
            public String getValue(PluginModelEx object) {
              return object.getPluginModel().getServiceName();
            }
        };       
        cellTable.addColumn(nameColumn, "Service Name");
        
        TextColumn<PluginModelEx> descriptionColumn = new TextColumn<PluginModelEx>(){
            @Override
            public String getValue(PluginModelEx object) {
              return object.getPluginModel().getDescription();
            }
        };       
        cellTable.addColumn(descriptionColumn, "Description");
  
        TextColumn<PluginModelEx> companyColumn = new TextColumn<PluginModelEx>(){
            @Override
            public String getValue(PluginModelEx object) {
              return object.getPluginModel().getProvider();
            }
        };       
        cellTable.addColumn(companyColumn, "Provider");
        
        TextColumn<PluginModelEx> licenseColumn = new TextColumn<PluginModelEx>(){
            @Override
            public String getValue(PluginModelEx object) {
              return object.getPluginModel().getLicense().toString();
            }
        };       
        cellTable.addColumn(licenseColumn, "License");  
        
        Column<PluginModelEx, String> detailColumn = new Column<PluginModelEx, String>(new ButtonCell()) {

                    @Override
                    public String getValue(PluginModelEx object) {
                        return "Detail";
                    }
        };
        cellTable.addColumn(detailColumn, "Detail");  
        detailColumn.setFieldUpdater(new FieldUpdater<PluginModelEx, String>() {

            public void update(int index, PluginModelEx object, String value) {
                showDetail(object);
            }
            
        });
/*        TextColumn<PluginModelEx> versionColumn = new TextColumn<PluginModelEx>(){
            @Override
            public String getValue(PluginModelEx object) {
              return object.getServiceVersion();
            }
        };       
        cellTable.addColumn(versionColumn, "Version");  

        TextColumn<PluginModelEx> urlColumn = new TextColumn<PluginModelEx>(){
            @Override
            public String getValue(PluginModelEx object) {
              return object.getServiceUrl();
            }
        };       
        cellTable.addColumn(urlColumn, "URL"); */ 
            
        final SingleSelectionModel<PluginModelEx> selectionModel = new SingleSelectionModel<PluginModelEx>();
        cellTable.setSelectionModel(selectionModel);
        selectionModel.addSelectionChangeHandler(
            new SelectionChangeEvent.Handler() {
                public void onSelectionChange(SelectionChangeEvent event) {
                    PluginModelEx selected = selectionModel.getSelectedObject();
                    if(selected != null && event.getSource() instanceof ButtonCell){
                       showDetail(selected);
                    }
                }
            }
        );
               
    }

    protected void showDetail(PluginModelEx selected) {
/*        new BasicDialog(new PluginDetail(selected)) {

            @Override
            protected void onCancel() {
                // TODO Auto-generated method stub
                
            }

            @Override
            protected void onOK(Composite composite) {
                // TODO Auto-generated method stub
                
            }
            
        }.showDialog();*/
    }
    
    protected void removePlugin(final Key key) {
        new BasicDialog("Delete this plugin?") {

            @Override
            protected void onOK(Composite composite) {
                DataService.Util.getInstance().removePlugin(key, new AsyncCallback<Void>() {
                    public void onFailure(Throwable caught) {

                    }

                    public void onSuccess(Void result) {
                        initialize();
                    }
                    
                });
            }
            
        }.showDialog();
        

    }

    public void setText(String text) {

    }

    public String getText() {
        return "";
    }

    public Widget getWidgetUi() {
        return this;
    }

    public List<PluginModelEx> getData() {
        return this.data;
    }

    public void addData(List<PluginModelEx> result) {
        this.data.addAll(result);
        //cellTable.setRowCount(result.size(), true);
        cellTable.setRowData(0, this.data); 
    }

    public void clear() {
        this.data.clear();
    }

    public void setDialogHandler(DialogHandler handler) {
        // TODO Auto-generated method stub
        
    }

    public Composite getComposite() {
        return this;
    }

}
